using System;
using TimeSheetDAL;
using System.Data;

namespace TimeSheetBLL.Brokers
{
	/// <summary>
	/// Summary description for Broker.
	/// </summary>
	public class Broker : IBroker
	{
		
		protected DataAccess data;
		protected BrokerTools tools;

		public Broker()
		{
			data = new DataAccess();
			tools = new BrokerTools();
		}
		
		public virtual long Save( IDBEntity entity )
		{
			if( entity.ID > 0 )
			{
				data.ExecuteNonQuery( tools.DBUpdateQuery( entity ) );
				return 0;
			}
			else
			{
				return data.InsertAutoNumber( tools.DBInsertQuery( entity, true ), tools.TableName( entity ) );
			}
		}

		public virtual bool Delete( IDBEntity entity )
		{
			return data.ExecuteNonQuery( tools.DBDeleteSingleQuery( entity ) ) > 0;
		}

		public virtual bool Fill( IDBEntity entity )
		{
			return data.Fill( entity, tools.DBSelectSingleQuery( entity ) );
		}

		public virtual DataTable SelectAll( IDBEntity entity )
		{
			return data.GetTable( "SELECT * FROM " + tools.TableName( entity ) );
		}

	

	}//class
}//namespace
